package org.jeecg.modules.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.mes.entity.MesProductionPlanOrderDetail;
import org.jeecg.modules.mes.service.IMesProductionPlanOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 生产计划订单明细关联管理
 * @Author: jeecg-boot
 * @Date: 2025-08-30
 * @Version: V2.0
 */
@Api(tags="生产计划订单明细关联管理")
@RestController
@RequestMapping("/mes/planOrderDetail")
@Slf4j
public class MesProductionPlanOrderDetailController extends JeecgController<MesProductionPlanOrderDetail, IMesProductionPlanOrderDetailService> {

    @Autowired
    private IMesProductionPlanOrderDetailService planOrderDetailService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "生产计划订单明细关联-分页列表查询")
    @ApiOperation(value="生产计划订单明细关联-分页列表查询", notes="生产计划订单明细关联-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<MesProductionPlanOrderDetail>> queryPageList(MesProductionPlanOrderDetail planOrderDetail,
                                                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                                    HttpServletRequest req) {
        QueryWrapper<MesProductionPlanOrderDetail> queryWrapper = QueryGenerator.initQueryWrapper(planOrderDetail, req.getParameterMap());
        Page<MesProductionPlanOrderDetail> page = new Page<MesProductionPlanOrderDetail>(pageNo, pageSize);
        IPage<MesProductionPlanOrderDetail> pageList = planOrderDetailService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据计划ID获取关联的订单明细
     */
    @AutoLog(value = "根据计划ID获取关联的订单明细")
    @ApiOperation(value="根据计划ID获取关联的订单明细", notes="根据计划ID获取关联的订单明细")
    @GetMapping(value = "/getByPlanId/{planId}")
    public Result<List<MesProductionPlanOrderDetail>> getOrderDetailsByPlanId(@PathVariable("planId") String planId) {
        List<MesProductionPlanOrderDetail> list = planOrderDetailService.getOrderDetailsByPlanId(planId);
        return Result.OK(list);
    }

    /**
     * 根据订单明细ID获取关联的计划
     */
    @AutoLog(value = "根据订单明细ID获取关联的计划")
    @ApiOperation(value="根据订单明细ID获取关联的计划", notes="根据订单明细ID获取关联的计划")
    @GetMapping(value = "/getByOrderDetailId/{orderDetailId}")
    public Result<List<MesProductionPlanOrderDetail>> getPlansByOrderDetailId(@PathVariable("orderDetailId") String orderDetailId) {
        List<MesProductionPlanOrderDetail> list = planOrderDetailService.getPlansByOrderDetailId(orderDetailId);
        return Result.OK(list);
    }

    /**
     * 批量创建计划订单明细关联
     */
    @AutoLog(value = "批量创建计划订单明细关联")
    @ApiOperation(value="批量创建计划订单明细关联", notes="批量创建计划订单明细关联")
    @PostMapping(value = "/batchCreate")
    public Result<Void> batchCreatePlanOrderDetails(@RequestParam("planId") String planId,
                                                   @RequestParam("orderDetailIds") List<String> orderDetailIds) {
        boolean result = planOrderDetailService.batchCreatePlanOrderDetails(planId, orderDetailIds);
        if (result) {
            return Result.OK("批量创建成功");
        } else {
            return Result.error("批量创建失败");
        }
    }

    /**
     * 更新订单明细生产进度
     */
    @AutoLog(value = "更新订单明细生产进度")
    @ApiOperation(value="更新订单明细生产进度", notes="更新订单明细生产进度")
    @PostMapping(value = "/updateProgress")
    public Result<Void> updateOrderDetailProgress(@RequestParam("planId") String planId,
                                                 @RequestParam("orderDetailId") String orderDetailId,
                                                 @RequestParam("actualQuantity") Integer actualQuantity) {
        boolean result = planOrderDetailService.updateOrderDetailProgress(planId, orderDetailId, actualQuantity);
        if (result) {
            return Result.OK("更新进度成功");
        } else {
            return Result.error("更新进度失败");
        }
    }

    /**
     * 获取计划的订单明细统计信息
     */
    @AutoLog(value = "获取计划的订单明细统计信息")
    @ApiOperation(value="获取计划的订单明细统计信息", notes="获取计划的订单明细统计信息")
    @GetMapping(value = "/getStats/{planId}")
    public Result<MesProductionPlanOrderDetail> getPlanOrderDetailStats(@PathVariable("planId") String planId) {
        MesProductionPlanOrderDetail stats = planOrderDetailService.getPlanOrderDetailStats(planId);
        return Result.OK(stats);
    }

    /**
     * 检查订单明细是否可以添加到计划中
     */
    @AutoLog(value = "检查订单明细是否可以添加到计划中")
    @ApiOperation(value="检查订单明细是否可以添加到计划中", notes="检查订单明细是否可以添加到计划中")
    @PostMapping(value = "/canAdd")
    public Result<Boolean> canAddOrderDetailsToPlan(@RequestParam("planId") String planId,
                                                   @RequestParam("orderDetailIds") List<String> orderDetailIds) {
        boolean canAdd = planOrderDetailService.canAddOrderDetailsToPlan(planId, orderDetailIds);
        return Result.OK(canAdd);
    }

    /**
     * 移除计划中的订单明细
     */
    @AutoLog(value = "移除计划中的订单明细")
    @ApiOperation(value="移除计划中的订单明细", notes="移除计划中的订单明细")
    @PostMapping(value = "/remove")
    public Result<Void> removeOrderDetailsFromPlan(@RequestParam("planId") String planId,
                                                  @RequestParam("orderDetailIds") List<String> orderDetailIds) {
        boolean result = planOrderDetailService.removeOrderDetailsFromPlan(planId, orderDetailIds);
        if (result) {
            return Result.OK("移除成功");
        } else {
            return Result.error("移除失败");
        }
    }

    /**
     * 添加
     */
    @AutoLog(value = "生产计划订单明细关联-添加")
    @ApiOperation(value="生产计划订单明细关联-添加", notes="生产计划订单明细关联-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody MesProductionPlanOrderDetail planOrderDetail) {
        planOrderDetailService.save(planOrderDetail);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     */
    @AutoLog(value = "生产计划订单明细关联-编辑")
    @ApiOperation(value="生产计划订单明细关联-编辑", notes="生产计划订单明细关联-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody MesProductionPlanOrderDetail planOrderDetail) {
        planOrderDetailService.updateById(planOrderDetail);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "生产计划订单明细关联-通过id删除")
    @ApiOperation(value="生产计划订单明细关联-通过id删除", notes="生产计划订单明细关联-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        planOrderDetailService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "生产计划订单明细关联-批量删除")
    @ApiOperation(value="生产计划订单明细关联-批量删除", notes="生产计划订单明细关联-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.planOrderDetailService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "生产计划订单明细关联-通过id查询")
    @ApiOperation(value="生产计划订单明细关联-通过id查询", notes="生产计划订单明细关联-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<MesProductionPlanOrderDetail> queryById(@RequestParam(name="id",required=true) String id) {
        MesProductionPlanOrderDetail planOrderDetail = planOrderDetailService.getById(id);
        if(planOrderDetail==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(planOrderDetail);
    }
}
